package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 李川
 * @since 2020-05-12
 */
@Api(description = "讲师管理")
@RestController
//@CrossOrigin  //解决跨域
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
   //1.查询讲师表中所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping(value = "/findAll",produces = "application/json;charset=utf-8")
    public R findAllTeacher(){
        List<EduTeacher> list= eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    //2.逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name="id",value = "讲师ID",required = true)
            @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }
        return R.error();
    }
    //3.分页查询讲师列表
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public  R pageListTeacher(
            @ApiParam(name="current",value = "当前页",required = true)
            @PathVariable long current,
            @ApiParam(name="limit",value = "页面数大小",required = true)
            @PathVariable long limit){
        //创建page对象
        Page<EduTeacher> teacherPage=new Page<>(current,limit);
       //查询分页对象
        eduTeacherService.page(teacherPage,null);
        long total=teacherPage.getTotal(); //总记录数
        List<EduTeacher> records=teacherPage.getRecords(); //数据集合
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);  //链式编程也可以
    }

    //4.条件分页查询
    @ApiOperation(value = "带条件分页查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public  R pageTeacherCondition(
            @ApiParam(name="current",value = "当前页",required = true)
            @PathVariable long current,
            @ApiParam(name="limit",value = "页面数大小",required = true)
            @PathVariable long limit,
            @ApiParam(name="teacherQuery",value = "分页查询条件",required = true)
            @RequestBody(required = false)   TeacherQuery teacherQuery){  //参数值可以没有但是要post请求
        //创建page对象
        Page<EduTeacher> teacherPage=new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper =new QueryWrapper<>();
        //多条件组合查询  ---动态sql
        String name=teacherQuery.getName();
        Integer level=teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        //条件查询分页对象
        eduTeacherService.page(teacherPage,wrapper);
        long total=teacherPage.getTotal(); //总记录数
        List<EduTeacher> records=teacherPage.getRecords(); //数据集合
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);  //链式编程也可以
    }

    //5.添加讲师数据（自动填充mp的时间戳）
    @ApiOperation(value = "添加讲师数据")
    @PostMapping("addTeacher")
    public R addTeacher(  @ApiParam(name="eduTeacher",value = "讲师个人数据",required = true)
                              @RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //6.根据讲师id进行查询
    @ApiOperation(value = "讲师id进行查询")
    @GetMapping("getTeacher/{id}")
    public R getTeacher( @ApiParam(name="id",value = "讲师ID",required = true)
                             @PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    //讲师修改功能
    @ApiOperation(value = "讲师修改功能")
    @PostMapping("updateTeacher")
    public R updateTeacher(
            @ApiParam(name="eduTeacher",value = "讲师个人数据",required = true)
            @RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

